package com.sinopec.handdevice.view

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.constants.OperationTypeEnum
import com.sinopec.basemodule.dialog.CommonTipsPopupWindow
import com.sinopec.basemodule.model.StockDataItemModel
import com.sinopec.basemodule.model.StockOutListModel
import com.sinopec.basemodule.launchcontract.UHFOutResultLaunch
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.handdevice.R
import com.sinopec.handdevice.contract.StockOutDetailContract
import com.sinopec.handdevice.presenter.StockOutDetailPresenter
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_stock_out_detail.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: StockOutDetailActivity
 * @Description: 出库详情
 */
@Route(path = ARouterManagerUtils.GOTO_MAIN_STOCK_OUT_DETAIL_ACTIVITY)
class StockOutDetailActivity : BaseActivity<StockOutDetailPresenter?>(),
    StockOutDetailContract.View {
    //提示语弹窗
    private lateinit var commonTipsPopupWindow: CommonTipsPopupWindow

    //data数据集合
    private var stockDataItemModels: ArrayList<StockDataItemModel> = arrayListOf()
    private var stockDataItemAdapter: CommonAdapter<StockDataItemModel>? = null

    private val mStockOutListModel by lazy {
        intent.getParcelableExtra("stockOutListModel") ?: StockOutListModel()
    }

    /**
     * 出库扫码启动器
     */
    private val uhfOutLauncher by lazy {
        registerForActivityResult(UHFOutResultLaunch) {
            if (it != null && it) {
                finish()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        mPresenter = StockOutDetailPresenter(this)
        stockDataItemModels.add(
            StockDataItemModel(
                "物料名称",
                mStockOutListModel.mtrName.toString()
            )
        )
        stockDataItemModels.add(
            StockDataItemModel(
                "司机姓名",
                mStockOutListModel.driverName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "身份证号码",
                mStockOutListModel.driverIdCard.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "车牌号",
                mStockOutListModel.vehiclePlateNo.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "售达方",
                mStockOutListModel.soldToParty.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "送达方",
                mStockOutListModel.shipToParty.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "企业名称",
                mStockOutListModel.enterpriseName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "交互标识",
                mStockOutListModel.outboundIntegrationId.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "销售订单号",
                mStockOutListModel.salesOrderNo.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "预约提货单号",
                mStockOutListModel.appointmentPickNo.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "来源节点-库位",
                mStockOutListModel.fromLocationName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "目的节点-客户",
                mStockOutListModel.toCustomer.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "发货时间",
                mStockOutListModel.shipmentTime.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "预约提货时间",
                mStockOutListModel.appointmentPickTime.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "仓库编码",
                mStockOutListModel.warehouseCode.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "仓库名称",
                mStockOutListModel.warehouseName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "仓库类型",
                if (mStockOutListModel.warehouseType == 1) "平面库" else "立体库"
            )
        )

        //销售订单号
        tv_salesOrderNo.text = "任务单号：" + mStockOutListModel.salesOrderNo
        stockDataItemAdapter = object : CommonAdapter<StockDataItemModel>(mContext,
            R.layout.activity_stock_out_detail_data_item,
            stockDataItemModels) {
            override fun convert(
                holder: CustomViewHolder,
                model: StockDataItemModel,
                position: Int,
            ) {
                val itemView = holder.itemView
                //类别名
                val tv_typeName = holder.getView<TextView>(R.id.tv_typeName)
                tv_typeName.text = model.typeName
                //类别值
                val tv_typeValue = holder.getView<TextView>(R.id.tv_typeValue)
                tv_typeValue.text = model.typeValue
                if (position % 2 == 0) {
                    itemView.setBackgroundColor(Color.parseColor("#ffffff"))
                } else {
                    itemView.setBackgroundColor(Color.parseColor("#F8F8F8"))
                }
            }
        }
        rv_list_data.adapter = stockDataItemAdapter
    }

    override fun getContentView(): Int {
        return R.layout.activity_stock_out_detail
    }

    override fun setStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    override fun configToolbar(rToolbar: CustomToolbar) {
        //配置标题
        rToolbar.setTitle("出库详情")
    }

    override fun onResume() {
        super.onResume()

    }

    override fun initEvent() {
        super.initEvent()

        /**
         * 出库扫码启动器
         */
        uhfOutLauncher

        /**
         * 确认
         */
        addDisposable(tv_confirm
            .clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                showCommonTipsPopupWindow(
                    0,
                    "提示",
                    "是否确认出库完成?",
                    "确认",
                    "取消",
                    OperationTypeEnum.confirm
                )
            })

        /**
         * 扫码
         */
        addDisposable(ll_scan
            .clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                uhfOutLauncher.launch(mStockOutListModel)
            })
    }

    /**
     * @author lwj
     * @Description: 二次确认通用弹窗
     */
    private fun showCommonTipsPopupWindow(
        @DrawableRes resId: Int,
        title: String,
        content: String,
        confirmText: String,
        cancelText: String,
        operationType: OperationTypeEnum,
    ) {
        commonTipsPopupWindow = CommonTipsPopupWindow.create(
            mContext,
            resId,
            title,
            content,
            confirmText,
            cancelText,
            object : CommonTipsPopupWindow.OnSelectListener {
                override fun onConfirm() {
                    mStockOutListModel.outboundIntegrationId?.let {
                        mPresenter?.confirmStockOutOrderFinish(it)
                    }
                    commonTipsPopupWindow.dismiss()
                }

                override fun onCancel() {
                    commonTipsPopupWindow.dismiss()
                }
            }).setAnchorView(mContext.rootView)
            .apply()
        commonTipsPopupWindow.showAtLocation(mContext.rootView, Gravity.CENTER, 0, 0)
    }
}